package com.pxmeta.common.config;


import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.app.AppService;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class WxPayConfig {

    public static Config config = null;

    @Value("${wx.pay.mchId}")
    private String merchantId;

    @Value("${wx.pay.keyPath}")
    private String privateKeyPath;

    @Value("${wx.pay.merchantSerialNumber}")
    private String merchantSerialNumber;

    @Value("${wx.pay.apiV3key}")
    private String apiV3key;

    @Bean
    public AppService appService(){
        if (config == null){
            config =
                    new RSAAutoCertificateConfig.Builder()
                            .merchantId(merchantId)
                            .privateKeyFromPath(privateKeyPath)
                            .merchantSerialNumber(merchantSerialNumber)
                            .apiV3Key(apiV3key)
                            .build();
        }

        return new AppService.Builder().config(config).build();
    }


    @Bean
    public JsapiService jsapiService(){
        if (config == null){
            config =
                    new RSAAutoCertificateConfig.Builder()
                            .merchantId(merchantId)
                            .privateKeyFromPath(privateKeyPath)
                            .merchantSerialNumber(merchantSerialNumber)
                            .apiV3Key(apiV3key)
                            .build();
        }


        return new JsapiService.Builder().config(config).build();
    }
}
